/* eslint-disable global-require */
const program = require('commander');

const path = require('path');
// eslint-disable-next-line import/no-unresolved
const { version } = require('./constants');

const mapAction = {
  create: {
    alias: 'c',
    description: 'create a project',
    examples: [
      'zhu-cli create <project-name>',
    ],
  },
  config: {
    alias: 'conf',
    description: 'config project variable',
    examples: [
      'zhu-cli config set <k> <v>',
      'zhu-cli config get <k>',
    ],
  },
  '*': {
    alias: '',
    description: 'command not found',
    examples: [],
  },
};

Reflect.ownKeys(mapAction).forEach((action) => {
  program.command(action)
    .alias(mapAction[action].alias)
    .description(mapAction[action].description)
    .action(() => {
      if (action === '*') {
        console.log(mapAction[action].description);
      } else {
        console.log(action);

        // eslint-disable-next-line global-require
        // eslint-disable-next-line import/no-dynamic-require
        require(path.resolve(__dirname, action))(...process.argv.slice(3));
      }
    });
});

program.on('--help', () => {
  console.log('\r\nExamples:');
  Reflect.ownKeys(mapAction).forEach((action) => {
    // eslint-disable-next-line arrow-parens
    mapAction[action].examples.forEach(example => {
      console.log(`   ${example}`);
    });
  });
});

program.version(version).parse(process.argv);
